package com.dlx.mem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dlx.common.result.Result;
import com.dlx.mem.service.MemInfoService;
import com.dlx.model.mem.MemInfo;
import com.dlx.vo.mem.MemInfoQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author nglon
 * @since 2021-11-08
 */
@Api("会员管理服务")
@RestController
@RequestMapping("/admin/mem/info")
@CrossOrigin
public class MemInfoController {

    @Autowired
    private MemInfoService memInfoService;

    //查询会员信息表所有数据
    @ApiOperation(value = "所有会员信息列表")
    @GetMapping("findAll")
    public Result findAll() {
        List<MemInfo> list = memInfoService.list();
        return Result.ok(list);
    }

    //逻辑删除会员信息
    @ApiOperation(value = "逻辑删除会员信息")
    @DeleteMapping("{id}")
    public Result removeMemInfo(@PathVariable Long id) {
        boolean isDelete = memInfoService.removeById(id);
        if (isDelete) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //分页条件查询会员信息
    @PostMapping("{current}/{limit}")
    public Result pageListMemInfo(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  MemInfoQueryVo memInfoQueryVo) {
        //调用方法实现分页查询
        IPage<MemInfo> page = this.memInfoService.selectMemInfoPage(new Page<>(current, limit), memInfoQueryVo);
        return Result.ok(page);
    }

    //修改会员信息
    @PostMapping("updateMemInfo")
    public Result updateMemInfo(@RequestBody MemInfo memInfo) {
        boolean flag = memInfoService.updateById(memInfo);
        return flag ? Result.ok() : Result.fail();
    }

    //更改会员账号可用状态
    @PatchMapping("lock/{id}/{isDisabled}")
    public Result lock(@PathVariable Long id, @PathVariable Boolean isDisabled) {
        memInfoService.lock(id, isDisabled);
        return Result.ok();
    }

    //查询会员详细信息
    @GetMapping("show/{id}")
    public Result show(@PathVariable Long id) {
        MemInfo memInfo = memInfoService.show(id);
        return Result.ok(memInfo);
    }

    //新增会员信息
    @PostMapping("add")
    public Result add(@RequestBody MemInfo memInfo) {
        if (!StringUtils.hasText(memInfo.getMobile()) || !StringUtils.hasText(memInfo.getPassword())) {
            return Result.fail().message("会员手机号码和会员密码不能为空!");
        }
        this.memInfoService.add(memInfo);
        return Result.ok();
    }

    @GetMapping("getById/{id}")
    public MemInfo getById(@PathVariable Long id) {
        return this.memInfoService.getById(id);
    }

    @GetMapping("/getByUserId/{userId}")
    public MemInfo getByUserId(@PathVariable Long userId) {
        return this.memInfoService.getByUserId(userId);
    }

    @PostMapping("update")
    public Result update(@RequestBody MemInfo memInfo) {
        boolean flag = this.memInfoService.updateById(memInfo);
        System.out.println(flag);
        return flag ? Result.ok() : Result.fail();
    }

    @GetMapping("updateMemInfoBalanceAndTotalRecharge/{id}/{memInfoBalance}/{memInfoTotalRecharge}")
    public Result updateMemInfoBalanceAndTotalRechargeById(@PathVariable Long id, @PathVariable BigDecimal memInfoBalance, @PathVariable BigDecimal memInfoTotalRecharge) {
        MemInfo memInfo = new MemInfo();
        memInfo.setId(id);
        memInfo.setBalance(memInfoBalance);
        memInfo.setTotalRecharge(memInfoTotalRecharge);
        boolean flag = this.memInfoService.updateById(memInfo);
        return flag ? Result.ok() : Result.fail();
    }

    @PostMapping("updateMemInfoByUserId")
    public Result updateMemInfoByUserId(@RequestBody MemInfo memInfo) {
        Boolean flag = this.memInfoService.updateByUserId(memInfo);
        return flag ? Result.ok() : Result.fail();
    }

}


